<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		$month = date('m');
		$year = date('Y');
		$last_date = cal_days_in_month(CAL_GREGORIAN, $month, $year);
		$start = strtotime('01-'.$month.'-'.$year.' 00:00:00');
		$finish = strtotime($last_date.'-'.$month.'-'.$year.' 23:59:59');
		
		$this->load->model('admin/news_model','news_model');
		$lists = $this->news_model->list_month($start,$finish);;
		$i = 1;
		$news_list['news_list_left'] = '';
		$news_list['news_list_right'] = '';
		$news = array();
		$siteurl = base_url();
		foreach ($lists as $v) {
			$news[($i-1)]['title'] = $v->n_title;
			$news[($i-1)]['start'] = $v->n_start;
			$news[($i-1)]['finish'] = $v->n_finish;
			
			$date_text = date('d M',$v->n_start).' - '.date('d M',$v->n_finish);
			if($i<=2){
				$news_list['news_list_left'] .= image_asset("calendar_icon_{$i}.png", '', array('alt'=>"{$v->n_title}"))." {$date_text} {$v->n_title}<br />";
			}else if($i<=4){
				$news_list['news_list_right'] .= image_asset("calendar_icon_{$i}.png", '', array('alt'=>"{$v->n_title}"))." {$date_text} {$v->n_title}<br />";
			}
			$i++;
		}
		
		$i = 1;
		$news_list['carlendar_left'] = '';
		$news_list['carlendar_right'] = '';
		for($i;$i<=$last_date;$i++){
			$day = $i;
			if(strlen($i)==1){
				$day = '0'.$day;
			}
			
			$pin = '';
			foreach ($news as $k=>$v) {
				$k = $k+1;
				if(strtotime("$day-$month-$year 12:12:12") >= $v['start'] AND strtotime("$i-$month-$year 12:12:12") <= $v['finish']){
					$pin .= image_asset("calendar_icon_{$k}.png", '', array('alt'=> $v['title'])).'<br />';
				}
			}
			
			$date_text = date('d/M',strtotime($day.'-'.$month.'-'.$year));
			
			if(($i>=1 AND $i<=5) OR ($i>=11 AND $i<=15) OR ($i>=21 AND $i<=25)){ // ฝั่งซ้าย
					$news_list['carlendar_left'] .=<<<ddd
						<div class="calendar_left" id="calendar_box_{$i}">
							<div class="date">{$date_text}</div>
							
							<div class="calendar_icon">
								{$pin}
							</div>
						</div>
ddd;
			}else{ //ฝั่งขวา
				if($last_date==31 AND $i==30){ //เป็นเดือนที่มี 31 วัน และเป็นวันที่ 30 ต้องเปิดท้ายรอวันที่ 31 มาเสียบ
					$news_list['carlendar_right'] .=<<<ddd
						<div class="calendar_left" id="calendar_box_{$i}">
							<div class="date date_{$i}">{$date_text}</div>
							
							<div class="calendar_icon calendar_icon_{$i}">
								{$pin}
							</div>
ddd;
				}else if($last_date==31 AND $i==31){ //เป็นวันที่ 31 ต้องเอาใส่กับวันที่ 30
					$news_list['carlendar_right'] .=<<<ddd
							<div class="date date_{$i}">{$date_text}</div>
							<div class="calendar_icon calendar_icon_{$i}">
								{$pin}
							</div>
						</div>
ddd;
				}else{
					$news_list['carlendar_right'] .=<<<ddd
						<div class="calendar_left" id="calendar_box_{$i}">
							<div class="date">{$date_text}</div>
							
							<div class="calendar_icon">
								{$pin}
							</div>
						</div>
ddd;
				}
			}
		}
		
		
		$data['title'] = 'Bake a wish : Japanese Homemade Cake';
		$data['description'] = 'Bake a wish : Japanese Homemade Cake';
		$data['keywords'] = 'Bake a wish : Japanese Homemade Cake';
		$data['menu1'] = '';
		$data['menu2'] = 'active';
		$data['menu3'] = '';
		$data['menu4'] = '';
		$data['menu5'] = '';
		$data['menu6'] = '';
		$this->load->view('f_header_view.php',$data);
		$this->load->view('f_news_view.php',$news_list);
		$this->load->view('f_footer_view.php');
	}
}

/* End of file news.php */
/* Location: ./application/controllers/news.php */